#include "stu.h"
linklist list;

int main(void)
{
    int a;
    InitList(); // 初始化头节点
    linklist head = list;
    linklist tail = list;
    TailInsertElem(&tail, 1, "Tom");

    for (int i = 0; i < 2; i++)
    {
        TailInsertElem(&tail, i+4, "Hell");
    }
    
    printlist(list);
  
    // printf("%d\n", head->next);
    system("pause");
    return 0;
}

void InitList()
{
    list = (linklist) malloc(sizeof(struct Node));
    if (list == NULL)
    {
        exit(-1);
    }
    
    list->data = NULL;
    list->next = NULL;
}

void visit(Data *data)
{
    printf("%d %s\n", data->id, data->name);
}

void printlist(linklist list)
{
    linklist p = list->next;
    while (p != NULL)
    {
        visit(p->data);
        p = p->next;
    }
    putchar('\n');
}





// 头插法
/* void HeadInsertElem(linklist* head, int data)
{
    linklist tmp = (linklist) malloc(sizeof(Node));
    // 设置数据
    tmp->data = data;
    tmp->next = list->next;
    (*head)->next = tmp;
    
} */

// 尾插法
void TailInsertElem(linklist* tail, int id, char *name)
{
    // (*tail) 相当于 main 里的 tail
    // 如果不传入指针，则是传值
    // printf("old_tail -> %p, ", *tail);
    // linklist tmp = (linklist) malloc(sizeof(struct Node));
    Data *data = (Data*) malloc(sizeof(struct Data)); // 数据
    data->id = id;

    data->name = (char*) malloc(sizeof(char) * 10); // 分配姓名的空间
    data->name = name;

    linklist tmp = (linklist) malloc(sizeof(struct Node));
    tmp->data = data;


    (*tail)->next = tmp;
    tmp->next = NULL;
    (*tail) = tmp;
    
    // printf("tail -> %p, tmp -> %p\n", *tail, tmp);
}